import { createApp } from 'vue'
import App from './App.vue'
//引入路由
import router from '@/routers/index.js'
//引入UI
import Vant from 'vant'
import 'vant/lib/index.css'

//引入封装的axios
import request from '@/services/request'

//引入自定义封装的工具箱
import tools from './services/tools'

//y引入cookie插件
import VueCookies from 'vue3-cookies'

const app = createApp(App)
    .use(router)
    .use(Vant)
    .use(request)
    .use(VueCookies)
    .use(tools)
    .mount('#app')


//路由守卫,登录检测
router.beforeEach(async (to, from) => {
    var IsLogin = to.meta.IsLogin ? to.meta.IsLogin : false;

    if (IsLogin) {

        var result =await app.$login()
        //查询用户失败
        if (result.code == 0) {
            //保留他要去的地址
            app.$cookies.set('redirect', to.fullPath);

            //清除缓存
            app.$cookies.remove('business')
            app.$fail({
                message: result.msg,
                duration: 1000,
                onClose: () => {
                    //跳转页面
                    app.$router.push(result.url)
                    return false
                }
            })

            //跳转回登录页面
            // return {path: 'business/login'}
        }

        //如果用户存在，返回最新的数据，覆盖cookie
        app.$cookies.set('business', result.data)
    }
})
